package com.lbssoft.gpsbd.dao;

import java.util.List;

import com.lbssoft.gpsbd.model.vo.EventVo;
import org.apache.ibatis.annotations.*;

import com.lbssoft.gpsbd.model.Event;

@Mapper
public interface EventMapper {
	/**
	 * 增加事件
	 * @param event
	 */

	@Insert("INSERT INTO lbs_events(deviceId,type,attributes,positionid,geofenceId,address) VALUE(#{deviceId},#{type},#{attributes},#{positionId},#{geofenceId},#{address})")
	void addEvent(Event event);
	
	/**
	 * 根据用户获取事件列表
	 * @param uid
	 * @return
	 */
	@Select("SELECT t.id, t.deviceId, t.type, c. NAME AS geoName, d.latitude, d.longitude, t.time, t.isread ,t.address FROM lbs_events t LEFT JOIN lbs_user_device b ON b.deviceid = t.deviceid LEFT JOIN lbs_geofences c ON c.id = t.geofenceid LEFT JOIN lbs_positions d ON d.id = t.positionid WHERE b.userid = #{uid} ORDER BY t.id DESC")
	List<EventVo> getEvents(@Param("uid") long uid);

	/**
	 * 获取未读的事件数量
	 * @param uid
	 * @return
     */
	@Select("SELECT COUNT(*) as unReadCount FROM lbs_events t  LEFT JOIN lbs_user_device b ON b.deviceid = t.deviceid where b.userid =#{uid} AND `isread`=false")
	Integer getUnReadCount(@Param("uid") long uid);

	/**
	 * 设置事件为已读
	 * @param id
     */
	@Update("UPDATE lbs_events SET isread = true where id = #{id}")
	void readEvent(@Param("id") long id);

	/**
	 * 删除事件
	 * @param id
     */
	@Delete("DELETE FROM lbs_events where id = #{id}")
	void delete(@Param("id") long id);

	
}
